// pages/address/address.js
// 引入SDK核心类
var QQMapWX = require('../../libs/qqmap-wx-jssdk.min.js');
// 实例化API核心类
var qqmapsdk = new QQMapWX({
	key: 'OF7BZ-44PLF-7P3JX-NGDVD-OLBFZ-UVBZZ' //开发者秘钥key
});
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		city: '',
		street_number: '',
		searchNearby: '附近',
		address_reference: null
	},
	toCity: function(e) {
		wx.navigateTo({
			url: './city/city'
		})
	},
	toIndex: function(e) {
		// console.log(e)
		let address = e.currentTarget.dataset.address;
		wx.setStorageSync('address', address);
		wx.navigateBack({
			url: '../index/index',
		});
		
	},
	searchNearby: function(e) {
		this.__data__.searchNearby = e.detail.value;
		this.addressNearby();
	},
	addressNearby: function(e) {
		qqmapsdk.search({
			keyword: this.__data__.searchNearby || '附近',
			success: (res) => {
				let data = [];
				for (let i = 0; i < res.data.length; i++) {
					// this.__data__.address_reference.push(res.data[i].title);
					data.push(res.data[i].title);
				}
				this.setData({
					address_reference: data
				});
			}
		});
	},
	locationFun: function() {
		wx.getLocation({
			type: 'gcj02', // wgs84
			success: (res) => {
				qqmapsdk.reverseGeocoder({
					location: {
						latitude: res.latitude,
						longitude: res.longitude
					},
					success: (res) => {
						// 获取当前地址成功
						this.setData({
							city: res.result.address_component.city,
							street_number: res.result.address_component.street_number
						})
						this.addressNearby();
					},
					fail: () => {
						console.log('reverseGeocoder failed')
					}
				})
			},
			fail: () => {
				console.log('getLocation failed')
			}
		});
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {
		this.locationFun();
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
